package com.academic.system.service.impl;

import com.academic.system.model.Achievement;
import com.academic.system.model.User;
import com.academic.system.repository.AchievementRepository;
import com.academic.system.repository.UserRepository;
import com.academic.system.service.AchievementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 成果服务实现类
 */
@Service
public class AchievementServiceImpl implements AchievementService {

    @Autowired
    private AchievementRepository achievementRepository;
    
    @Autowired
    private UserRepository userRepository;

    @Override
    public Achievement findById(Integer id) {
        return achievementRepository.findById(id);
    }

    @Override
    public List<Achievement> findByStudentId(Integer studentId) {
        return achievementRepository.findByStudentId(studentId);
    }

    @Override
    public List<Achievement> findByReviewerId(Integer reviewerId) {
        return achievementRepository.findByReviewerId(reviewerId);
    }

    @Override
    public List<Achievement> findByStatus(String status) {
        return achievementRepository.findByStatus(status);
    }

    @Override
    public List<Achievement> findByStudentIdAndStatus(Integer studentId, String status) {
        return achievementRepository.findByStudentIdAndStatus(studentId, status);
    }

    @Override
    public List<Achievement> findByReviewerIdAndStatus(Integer reviewerId, String status) {
        return achievementRepository.findByReviewerIdAndStatus(reviewerId, status);
    }

    @Override
    public Achievement createAchievement(Achievement achievement) {
        achievement.setCreatedAt(LocalDateTime.now());
        achievement.setStatus("待审核");
        achievementRepository.insert(achievement);
        return achievement;
    }

    @Override
    public Achievement updateAchievement(Achievement achievement) {
        achievement.setUpdatedAt(LocalDateTime.now());
        achievementRepository.update(achievement);
        return achievement;
    }

    @Override
    public void deleteAchievement(Integer id) {
        achievementRepository.deleteById(id);
    }

    @Override
    public Achievement reviewAchievement(Integer id, String status, String comments, Integer reviewerId) {
        Achievement achievement = achievementRepository.findById(id);
        if (achievement != null) {
            achievement.setStatus(status);
            achievement.setComments(comments);
            achievement.setUpdatedAt(LocalDateTime.now());
            
            User reviewer = userRepository.findById(reviewerId);
            achievement.setReviewer(reviewer);
            
            achievementRepository.update(achievement);
        }
        return achievement;
    }

    @Override
    public Map<String, Integer> getStudentAchievementStats(Integer studentId) {
        Map<String, Integer> stats = new HashMap<>();
        
        // 获取总成果数
        int total = achievementRepository.countByStudentId(studentId);
        stats.put("total", total);
        
        // 获取已通过成果数
        int approved = achievementRepository.countByStudentIdAndStatus(studentId, "已通过");
        stats.put("approved", approved);
        
        // 获取待审核成果数
        int pending = achievementRepository.countByStudentIdAndStatus(studentId, "待审核");
        stats.put("pending", pending);
        
        // 获取已拒绝成果数
        int rejected = achievementRepository.countByStudentIdAndStatus(studentId, "已拒绝");
        stats.put("rejected", rejected);
        
        return stats;
    }
} 